package com.example.myaku.network;

import android.content.Context;
import android.net.nsd.NsdServiceInfo;
import android.net.nsd.NsdManager;
import android.util.Log;

//扫描ip的升级版，兼容Android10+
public class ServiceDiscoveryHelper {

    private NsdManager nsdManager;
    private NsdManager.DiscoveryListener discoveryListener;
    private OnServiceFoundListener listener;

    public interface OnServiceFoundListener {
        void onServiceFound(String host, int port);
    }

    public void setOnServiceFoundListener(OnServiceFoundListener listener) {
        this.listener = listener;
    }

    public void startDiscovery(Context context) {
        nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);

        discoveryListener = new NsdManager.DiscoveryListener() {

            @Override
            public void onStartDiscoveryFailed(String serviceType, int errorCode) {
                Log.e("Nsd", "Start discovery failed: " + errorCode);
            }

            @Override
            public void onStopDiscoveryFailed(String serviceType, int errorCode) {
                Log.e("Nsd", "Stop discovery failed: " + errorCode);
            }

            @Override
            public void onDiscoveryStarted(String serviceType) {
                Log.d("Nsd", "Discovery started");
            }

            @Override
            public void onDiscoveryStopped(String serviceType) {
                Log.d("Nsd", "Discovery stopped");
            }

            @Override
            public void onServiceFound(NsdServiceInfo serviceInfo) {
                Log.d("Nsd", "Service found: " + serviceInfo.getServiceName());
                if ("_http._tcp.".equals(serviceInfo.getServiceType())) {
                    nsdManager.resolveService(serviceInfo, new NsdManager.ResolveListener() {
                        @Override
                        public void onResolveFailed(NsdServiceInfo info, int errorCode) {
                            Log.e("Nsd", "Resolve failed");
                        }

                        @Override
                        public void onServiceResolved(NsdServiceInfo info) {
                            Log.d("Nsd", "Resolved: " + info.getHost().getHostAddress() + ":" + info.getPort());
                            if (listener != null) {
                                listener.onServiceFound(info.getHost().getHostAddress(), info.getPort());
                            }
                        }
                    });
                }
            }

            @Override
            public void onServiceLost(NsdServiceInfo serviceInfo) {
                Log.d("Nsd", "Service lost: " + serviceInfo.getServiceName());
            }
        };

        nsdManager.discoverServices("_http._tcp.", NsdManager.PROTOCOL_DNS_SD, discoveryListener);
    }

    public void stopDiscovery() {
        if (nsdManager != null && discoveryListener != null) {
            nsdManager.stopServiceDiscovery(discoveryListener);
        }
    }

}
